var express = require('express');
var router = express.Router();
var async=require('async');

var MongoClient=require('mongodb').MongoClient;

var DB_CONN_STR='mongodb://10.31.155.79:27017/happy';

/* GET home page. */
router.get('/', function(req, res, next) {
   var indexdata=function(db,callback){
     var conn_movie=db.collection('movielist');
     var conn_book=db.collection('book');
     var conn_music=db.collection('musiclist');

     async.parallel([
         function(callback){
             conn_movie.find({}).toArray(function(err,results){
                 if(err){
                   return;
                 }else{
                    callback(null,results);
                 }
             })
         },
         function(callback){
             conn_music.find({}).toArray(function(err,results){
                 if(err){
                   return;
                 }else{
                    callback(null,results);
                 }
             })
         },
         function(callback){
             conn_book.find({}).toArray(function(err,results){
                 if(err){
                   return;
                 }else{
                    callback(null,results);
                 }
             })
         }


     ],
         function(err,results){
             callback(results[0],results[1],results[2]);
           
         })

   }
      MongoClient.connect(DB_CONN_STR,function(err,db){
        if(err){
            console.log(err);
        }else{
           console.log('sussee');
           indexdata(db,function(movie,music,book){
                var movieArr=movie[0].movie_list;
                var musicArr=music[0].song_list;
                var bookArr=book[0].books;
                res.render('index',{
                    movieArr:movieArr,
                    musicArr:musicArr,
                    bookArr:bookArr
                })
           })
          
             
        }

   })


});




/* 注册页面 */
router.get('/register', function(req, res, next) {
  res.render('register', {config: 'none'});
});

/* 登录页面 */
router.get('/login', function(req, res, next) {
  res.render('login', { config:'none' });
});




module.exports = router;
